package com.library.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.library.bean.User;
import com.library.service.BookService;
import com.library.service.UserService;
import com.library.service.impl.BookServiceImpl;
import com.library.service.impl.UserServiceImpl;

public class LoginServlet extends HttpServlet {
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		

		UserService us = new UserServiceImpl();
		BookService bs = new BookServiceImpl();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		Map<String,Object> map = us.login(username, password);
		if(map.get("loginUser")!=null){
			User loginUser = (User)map.get("loginUser");
			HttpSession session = request.getSession();//servlet中 不能 直接使用session 需要通过 request.getSession()来获取
			session.setAttribute("loginUser", loginUser);
			response.sendRedirect("showBook.do");
		}else{
			request.setAttribute("errorCode", map.get("errorCode"));
			request.getRequestDispatcher("login.jsp").forward(request,response);
		}
	}
	

}
